home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.01 Jan 91 / PieMenus / Pie Source / plotcircle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-13  |  5.6 KB  |  340 lines  |  [TEXT/KAHL]

  1. /*
  2.  * File:    plotcircle.c
  3.  *
  4.  * Purpose: Construct three collections of points
  5.  *            representing concentric circles used
  6.  *            in the plotting of pie popup menus.
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11.  
  12.  
  13. #define    I_RADIUS    50        /* Inner radius    */
  14. #define    O_RADIUS    70        /* Outer radius    */
  15. #define    C_RADIUS    5        /* Center radius   */
  16. #define    CENTER_X    0        /* Bias all points */
  17. #define    CENTER_Y    0        /*  at origin 0,0. */
  18. #define    MAX_PTS        1000    /* Max # points    */
  19. #define    X_COORD        1
  20. #define    Y_COORD        2
  21. #define    I_POINTS    1
  22. #define    O_POINTS    2
  23. #define    C_POINTS    3
  24.  
  25.  
  26. /* This is the representation of a point. */
  27. struct    xy_pt {
  28.     int        x;
  29.     int        y;
  30. } pt[MAX_PTS];
  31.  
  32. /*
  33.  * Array index into which to generate the next 
  34.  * point. 
  35.  */
  36. int num_pts;
  37.  
  38.  
  39. /* File into which the data is written. */
  40. FILE    *fp;
  41.  
  42.  
  43. /* Local Function prototypes. */
  44. void circle(int xc, int y_center, int radius);
  45. void sortpoints(void);
  46. void sort(struct xy_pt *, int, int);
  47. int     compare(struct xy_pt, struct xy_pt, int);
  48. void swap(struct xy_pt *, struct xy_pt *);
  49.  
  50.  
  51.  
  52. int
  53. main()
  54. {
  55.     if ((fp=fopen("circledata.h","w")) != NULL) {
  56.         /* Generate the set of interior points */
  57.         circle(CENTER_X, CENTER_Y, I_RADIUS);    
  58.         sortpoints();
  59.         generate_quadrants();
  60.         dumppoints(I_POINTS);
  61.  
  62.         /* Generate the set of outer points */
  63.         circle(CENTER_X, CENTER_Y, O_RADIUS);
  64.         sortpoints();    
  65.         generate_quadrants();    
  66.         dumppoints(O_POINTS);    
  67.         
  68.         /* Generate the set of center points */
  69.         circle(CENTER_X, CENTER_Y, C_RADIUS);    
  70.         sortpoints();    
  71.         generate_quadrants();
  72.         dumppoints(C_POINTS);
  73.  
  74.         fclose(fp);
  75.     }
  76.     else {
  77.         fprintf(stderr,
  78.                 "Can't open circledata.h\n");
  79.         exit(-1);
  80.     }
  81.     
  82.     exit(0);
  83. }
  84.  
  85.  
  86.  
  87. void
  88. circle(x_center, y_center, radius)
  89.     int x_center, y_center ,radius;
  90. {
  91.     int     x,y,d;
  92.     
  93.     num_pts = 0;
  94.     y         = radius;
  95.     d         = 3 - 2 * radius;
  96.     
  97.     
  98.     /* 
  99.      * Use x,y to control the plotting of the
  100.      * first octant.
  101.      */
  102.     for (x = 0; x < y;) {
  103.         if (num_pts + 1 >= MAX_PTS)
  104.             return;
  105.         pt[num_pts].x   =  x + x_center;
  106.         pt[num_pts++].y = -y + y_center;
  107.         pt[num_pts].x   =  y + x_center;
  108.         pt[num_pts++].y = -x + y_center;
  109.         if (d < 0)
  110.             d += 4 * x + 6;
  111.         else {
  112.             d += 4 * (x - y) + 10;
  113.             --y;
  114.         }
  115.         ++x;
  116.     }
  117.     
  118.     /*
  119.      * If we happened to land on the last point,
  120.      * add it to the list.
  121.      */
  122.     if (x == y) {
  123.         pt[num_pts].x   =  x + x_center;
  124.         pt[num_pts++].y = -y + y_center;
  125.     }
  126.         
  127.     fprintf(fp,"\n");
  128. }
  129.  
  130.  
  131. void
  132. sortpoints()
  133. {
  134.     sort(pt,num_pts,X_COORD);
  135.     sort(pt,num_pts,Y_COORD);
  136. }
  137.  
  138.  
  139.  
  140. int
  141. generate_quadrants()
  142. {
  143.     int i;
  144.     
  145.     /* 
  146.      * Second quadrant same as first with
  147.      * positive y coordinates. Note that the end
  148.      * points are not duplicated.
  149.      */
  150.     for (i = num_pts - 1; i >= 0; --i) {
  151.         pt[num_pts].x =  pt[i].x;
  152.         pt[num_pts].y = -pt[i].y;
  153.         ++num_pts;
  154.     }
  155.     
  156.     
  157.     /* 
  158.      * 3rd & 4th quadrants same as 1st & 2nd with
  159.      * negative x coordinates.
  160.      */
  161.     for (i = num_pts - 1; i >= 0; --i) {
  162.         pt[num_pts].x = -pt[i].x;
  163.         pt[num_pts].y = pt[i].y;
  164.         ++num_pts;
  165.     }
  166. }
  167.  
  168.  
  169.  
  170. int
  171. dumppoints(type)
  172.     int    type;
  173. {
  174.     int     i;
  175.     int     cols = 0;
  176.         
  177.     /* Print out the title. */
  178.     fprintf(fp, 
  179.             "/*\n");
  180.     fprintf(fp,
  181.             " * Points of a circle with "
  182.             "origin at %d,%d ", 
  183.             CENTER_X, CENTER_Y);
  184.  
  185.     if (type == I_POINTS) {
  186.         fprintf(fp,
  187.                 "with a radius of: %d\n",
  188.                 I_RADIUS);
  189.         fprintf(fp,
  190.                 " */\n\n");
  191.         fprintf(fp,
  192.                 "#define\t\tCENTER_X\t\t\t%d\n", 
  193.                 CENTER_X);
  194.         fprintf(fp,
  195.                 "#define\t\tCENTER_Y\t\t\t%d\n\n", 
  196.                 CENTER_Y);
  197.         fprintf(fp,"#define\t\tI_RADIUS\t\t\t%d\n", 
  198.                 I_RADIUS);
  199.                         
  200.         fprintf(fp,
  201.                 "#define\t\tNUM_I_POINTS\t\t%d\n",
  202.                 num_pts);
  203.     }
  204.     else if (type == O_POINTS) {
  205.         fprintf(fp,
  206.                 "with a radius of: %d\n",
  207.                  O_RADIUS);
  208.         fprintf(fp,
  209.                 " */\n");
  210.         fprintf(fp,
  211.                 "#define\t\tO_RADIUS\t\t\t%d\n",
  212.                 O_RADIUS);
  213.         fprintf(fp,
  214.                 "#define\t\tNUM_O_POINTS\t\t%d\n",
  215.                 num_pts);
  216.     }        
  217.     else {
  218.         fprintf(fp,
  219.                 "with a radius of: %d\n",
  220.                 C_RADIUS);
  221.         fprintf(fp,
  222.                 " */\n");
  223.         fprintf(fp,
  224.                 "#define\t\tC_RADIUS\t\t\t%d\n",
  225.                 C_RADIUS);
  226.         fprintf(fp,
  227.                 "#define\t\tNUM_C_POINTS\t\t%d\n",
  228.                 num_pts);
  229.     }
  230.     
  231.     /*
  232.      * Print the structure description we are
  233.      * about to define.
  234.      */
  235.     if (type == I_POINTS) {
  236.         fprintf(fp,
  237.                 "\n\n/* This represents a point. */\n");
  238.         fprintf(fp,"struct\txy_pt {\n");
  239.         fprintf(fp,"\tint    x;\n");
  240.         fprintf(fp,"\tint    y;\n");
  241.         fprintf(fp,"};\n\n");
  242.         fprintf(fp,"struct xy_pt i_circle_points");
  243.         fprintf(fp,"[NUM_I_POINTS] = {\n\t");
  244.     }
  245.     else if (type == O_POINTS) {
  246.         fprintf(fp,"struct xy_pt o_circle_points");
  247.         fprintf(fp,"[NUM_O_POINTS] = {\n\t");
  248.     }
  249.     else {
  250.         fprintf(fp,"struct xy_pt c_circle_points");
  251.         fprintf(fp,"[NUM_C_POINTS] = {\n\t");
  252.     }
  253.     
  254.     for (i = 0; i < num_pts; i++) {
  255.         fprintf(fp,"{%5d,%5d}", pt[i].x, pt[i].y);
  256.         if (i != (num_pts - 1))
  257.             fprintf(fp, ",");
  258.         ++cols;
  259.         if ( (cols % 5) == 0 ) {
  260.             fprintf(fp,"\n");
  261.             cols = 0;
  262.         }
  263.         fprintf(fp, "\t");
  264.     }
  265.     
  266.     if (cols != 0)
  267.         fprintf(fp,"\n");
  268.         
  269.     fprintf(fp,"};\n");
  270. }
  271.  
  272.  
  273.  
  274. void
  275. sort(v, n, sort_field)
  276.     struct xy_pt v[];
  277.     int n;
  278.     int sort_field;
  279. {
  280.     int gap, i, j;
  281.     
  282.     for (gap = n/2; gap > 0; gap /= 2)
  283.       for (i = gap; i < n; i++)
  284.         for (j = i-gap; j >= 0; j -= gap) {
  285.           if (compare(v[j],v[j+gap],sort_field)<=0)
  286.             break;
  287.           swap(&v[j], &v[j+gap]);
  288.         }
  289. }
  290.  
  291.  
  292.  
  293. int
  294. compare(v1, v2, sort_field)
  295.     struct    xy_pt v1,v2;
  296.     int    sort_field;
  297. {
  298.     int    temp1,temp2;    
  299.     
  300.     /*
  301.      * When sorting Y coordinates, don't swap
  302.      * unless X's are equal
  303.      */
  304.     if ((sort_field == Y_COORD) && (v1.x != v2.x)) 
  305.         return (0);
  306.     
  307.     /* Extract the values to compare */
  308.     if (sort_field == X_COORD) {
  309.         temp1 = v1.x;
  310.         temp2 = v2.x;
  311.     }
  312.     else {
  313.         temp1 = v1.y;
  314.         temp2 = v2.y;
  315.     }
  316.     
  317.     /* Return the comparison of the two values */
  318.     if (temp1 < temp2)
  319.         return (-1);
  320.     else if (temp1 > temp2)
  321.         return (1);
  322.     else
  323.         return (0);
  324. }
  325.  
  326.  
  327.  
  328.  
  329. void
  330. swap(p1, p2)
  331.     struct xy_pt *p1, *p2;
  332. {
  333.     struct    xy_pt    temp;
  334.     
  335.     temp = *p1;
  336.     *p1 = *p2;
  337.     *p2 = temp;
  338. }
  339.  
  340.